package project.function;

import com.baidu.aip.face.AipFace;
import com.github.sarxos.webcam.WebcamUtils;
import com.github.sarxos.webcam.util.ImageUtils;
import org.json.JSONObject;
import project.interactive_face.Interactive_webcam;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;

public class Function_Webcam implements Interactive_webcam {
    public static String groupId = "03170011Test";
    public static String Image_type = "BASE64";
    public static String image = pick();

    //调用摄像头拍摄
    public static String pick(){
        String fileName = "";	//照片路径名
        WebcamUtils.capture(webcam, fileName, ImageUtils.FORMAT_PNG);
        return imageToBase64Str("");	//照片本地相对路径
    }

    //BASE64加密方法
    public static String imageToBase64Str(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        //BASE64Encoder encoder = new BASE64Encoder();
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(data);
    }

    //添加用户方法的options参数设置
    public static HashMap addUserOptions(){
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");
        options.put("action_type", "REPLACE");
        return options;
    }

    //注册人脸方法
    public static JSONObject addUser(String id){
        String ID = id;
        AipFace af = ApiKey.aipInit();
        return af.addUser(Function_Webcam.image, Function_Webcam.Image_type, Function_Webcam.groupId,ID,Function_Webcam.addUserOptions());
    }

    //人脸检测接口
    public static HashMap detectOptions(){
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");
        return options;
    }

    //人脸检测方法
    public static JSONObject detect(){
        AipFace af = ApiKey.aipInit();
        return af.detect(Function_Webcam.image, Function_Webcam.Image_type,Function_Webcam.detectOptions());
    }

    //人脸搜索接口
    public static HashMap searchOptions(){
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("max_face_num", "3");
        options.put("match_threshold", "70");
        options.put("quality_control", "LOW");
        options.put("liveness_control", "LOW");
        options.put("max_user_num", "3");
        return options;
    }

    //人脸搜索方法
    public static JSONObject search(){
        AipFace af = ApiKey.aipInit();
        return af.search(Function_Webcam.image, Function_Webcam.Image_type,Function_Webcam.groupId,Function_Webcam.searchOptions());
    }
}
